package com.foot.travel.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.foot.travel.R;
import com.foot.travel.R.id;
import com.foot.travel.R.layout;
import com.foot.travel.viewholder.SelectViewholder;
import com.koalac.ironman.common.adapter.CommonAdapter;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.pojo.response.ProductdateData;
import com.koalac.ironman.net.pojo.response.SchoolTravelDetail;
import com.koalac.ironman.net.pojo.response.TourDate;
import com.koalac.ironman.viewhelper.ViewInject;

public class SelectDateActivity extends ModuleActivity implements
	OnItemClickListener {

    public static final String DATE = "date";
    @ViewInject(id = R.id.listview, itemClick = "onItemClick")
    GridView mListView;
    CommonAdapter<TourDate> mTourDateAdapter;
    List<TourDate> mTourDateList = new ArrayList<TourDate>();
    public static final String PRODUCT_DATE_DATA_LIST = "productdateDataList";;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("选择日期");
	setBackButtonVisibility(true);
	initTourDateList();
    }

    private void initTourDateList() {
	SchoolTravelDetail schoolTravelDetail = (SchoolTravelDetail) getIntent()
		.getSerializableExtra(PRODUCT_DATE_DATA_LIST);
	List<ProductdateData> productdateDatas = schoolTravelDetail
		.getProductdateDataList();
	if(productdateDatas.size() == 0) {
	    toast("暂无出团日期!");
	    return;
	}
	for (int i = 0; i < productdateDatas.size(); i++) {
	    TourDate tourDate = new TourDate();
	    tourDate.setDate(productdateDatas.get(i).getStartDate());
	    mTourDateList.add(tourDate);
	}
	mTourDateAdapter = new CommonAdapter<TourDate>(SelectViewholder.class,
		mTourDateList, SelectDateActivity.this,
		R.layout.select_date_item);
	mListView.setAdapter(mTourDateAdapter);
    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_select_date);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
	    long id) {
	TourDate tourDate = mTourDateList.get(position);
	Intent intent = new Intent();
	intent.putExtra(DATE, tourDate.getDate());
	setResult(RESULT_OK, intent);
	SelectDateActivity.this.finish();
    }

}
